package com.study.packet;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName: NettyClientPacketHandler
 * @Description:
 * @Date 2024/04/29 16:02
 * @Author songshaoqiu
 **/
public class NettyClientPacketHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 200; i++) {
            String msg = "前锋教研员";
            //创建消息协议对象
            MessageProtocol messageProtocol = new MessageProtocol();
            //封装长度和内容
            byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
            messageProtocol.setLength(bytes.length);
            messageProtocol.setContent(bytes);
            //发送消息协议对象
            //ctx 只能发送bytebuf数据，因此要使用编码器 将messageProtocol 转换为byteBuf
            ctx.writeAndFlush(messageProtocol);

        }

    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {

    }
}
